package com.choosefine.base.shixiaobao.common.utils;
import java.util.Date;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

/**
 * Created by yeliang on 2016/11/28.
 */
public class CreateToken {


    public static String createJWT(String name,String role,String secretkey) {

        String token = Jwts.builder().setSubject(name).claim("role", role).setIssuedAt(new Date()).signWith(SignatureAlgorithm.HS256,secretkey).compact();
        return token;
    }

    public static String checkToken(String authorization,String secretkey) throws Exception {

        if (authorization == null || !authorization.startsWith("Bearer ")) {
            throw new Exception("Missing or invalid Authorization header.");
        }

        final String token = authorization.substring(7); // The part after "Bearer "
        Claims claims = Jwts.parser().setSigningKey("secretkey")
                .parseClaimsJws(token).getBody();
        return claims.get("role").toString();
    }
}
